<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        v-model="baTable.table.extend!.showBalanceDialog"
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    :model="form"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('shop.user.user.nickname')"
                        type="string"
                        v-model="userData.nickname"
                        prop="nickname"
                        :input-attr="{
                            disabled: true,
                        }"
                    />
                    <FormItem
                        :label="t('shop.user.user.before_edit_balance')"
                        type="number"
                        v-model="userData.money"
                        prop="money"
                        :input-attr="{
                            disabled: true,
                        }"
                    />
                    <InputNumber
                        :label="t('shop.user.user.edit_balance')"
                        v-model="form.money"
                        prop="edit_balance"
                        :input-attr="{
                            precision: 2,
                        }"
                        @change="changeMoney"
                    />
                    <FormItem
                        :label="t('shop.user.user.after_edit_balance')"
                        type="number"
                        v-model="form.after"
                        prop="after"
                        :input-attr="{
                            disabled: true,
                        }"
                    />
                    <FormItem
                        :label="t('shop.user.user.edit_balance_memo')"
                        type="textarea"
                        v-model="form.memo"
                        prop="memo"
                        :input-attr="{
                            rows: 5,
                        }"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button type="danger" @click="baTable.table.extend!.showBalanceDialog = false">取消</el-button>
                <el-button type="primary" @click="onSubmit(formRef)">确定</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import FormItem from '/@/components/formItem/index.vue'
import InputNumber from '/@/components/shop/form/inputNumber.vue'
import { editBalance } from '/@/api/backend/shop/user/user'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    memo: [buildValidatorData({ name: 'required', title: t('shop.user.user.edit_balance_memo') })],
})

const userData = baTable.table.extend!.userData

const form = reactive({
    money: 0,
    after: userData.money,
    memo: '',
})

const changeMoney = (value: string) => {
    if (!userData || typeof userData == 'undefined') {
        form.after = 0
        return
    }
    let newValue = value == '' ? 0 : parseFloat(value)
    form.after = (parseFloat(userData.money) + newValue).toFixed(2)
}

const onSubmit = (formEl: FormInstance | undefined = undefined) => {
    if (formEl) {
        formEl.validate((valid: boolean) => {
            if (valid) {
                editBalance({
                    user_id: userData.id,
                    money: form.money,
                    memo: form.memo,
                }).then(() => {
                    baTable.table.extend!.showBalanceDialog = false
                    baTable.onTableHeaderAction('refresh', {})
                })
            }
        })
    }
}
</script>

<style scoped lang="scss"></style>
